package com.seselin.stepdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Seselin on 2016/7/15.
 */
public class StepProcess extends View {

    public static final int STEP_DONE = 1;
    public static final int STEP_UNDO = 2;

    private int size = 1;
    private int state = STEP_UNDO;

    public void setState(int state) {
        this.state = state;
    }

    private final Paint paint;
    private final Context context;

    public StepProcess(Context context) {
        this(context, null);
    }

    public StepProcess(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.context = context;
        this.paint = new Paint();
        this.paint.setAntiAlias(true); // 消除锯齿
    }


    @Override
    protected void onDraw(Canvas canvas) {

        int wCenter = getWidth() / 2;
        int hCenter = getHeight() / 2;
        int innerCircle = dip2px(context, 2 * size); // 设置内圆半径

        this.paint.setStyle(Paint.Style.FILL); // 绘制实心圆
        this.paint.setStrokeWidth(7);
        if (state == STEP_DONE) {
            this.paint.setColor(Color.GREEN);
        } else if (state == STEP_UNDO) {
            this.paint.setColor(Color.DISABLE_GRAY);
        }

        int space = wCenter / 6;
        for (int i = 0; i < 6; i++) {
            canvas.drawCircle(space + i * 2 * space, hCenter, innerCircle, this.paint);
        }

        super.onDraw(canvas);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
